﻿using System;
using System.Media;
using System.IO;

namespace B2BCore
{
    /// <summary>
    /// Functions to play an audio file. File must be Wave Format.
    /// </summary>
    public class Audio
    {
        /// <summary>
        /// Plays a sound file
        /// </summary>
        /// <param name="file">The exact file location</param>
        public static void PlaySound(string file)
        {
            if (String.IsNullOrEmpty(file))
                throw new ArgumentException(ErrorStrings.ExMsgFileNullOrEmpty, Utilities.GetVariableName(file));

            SoundPlayer player;
            try
            {
                player = new SoundPlayer(@file);
                player.Play();
            }
            catch (FileNotFoundException e)
            {
                throw new FileNotFoundException(String.Format(ErrorStrings.FailPlayAudioMissing, file), e);
            }
            catch (InsufficientMemoryException e)
            {
                throw new InsufficientMemoryException(string.Format(ErrorStrings.FailPlayAudioMemory, file), e);
            }
        }

        /// <summary>
        /// Plays a sound for a set amount of loops
        /// </summary>
        /// <param name="file">The exact file location</param>
        /// <param name="numberofloops">The number of times to loop</param>
        public static void PlaySound(string file, uint numberofloops)
        {
            if (String.IsNullOrEmpty(file))
                throw new ArgumentException(ErrorStrings.ExMsgFileNullOrEmpty, Utilities.GetVariableName(file));

            SoundPlayer player;
            try
            {
                player = new SoundPlayer(@file);
                player.Load();
                for (int i = 0; i < numberofloops; i++)
                {
                    player.Play();
                }
            }
            catch (FileNotFoundException e)
            {
                throw new FileNotFoundException(String.Format(ErrorStrings.FailPlayAudioMissing, file), e);
            }
            catch (InsufficientMemoryException e)
            {
                throw new InsufficientMemoryException(string.Format(ErrorStrings.FailPlayAudioMemory, file), e);
            }
        }

        /// <summary>
        /// Play a resource
        /// </summary>
        /// <param name="resource">The name of the resource to play</param>
        public static void PlayResource(string resource)
        {
            if (String.IsNullOrEmpty(resource))
                throw new ArgumentException(ErrorStrings.ExMsgResourceNullOrEmpty, Utilities.GetVariableName(resource));

            try
            {
                System.Reflection.Assembly a = System.Reflection.Assembly.GetExecutingAssembly();
                Stream s = a.GetManifestResourceStream(resource);
                SoundPlayer player = new SoundPlayer(s);
                player.Play();
            }
            catch (FileNotFoundException e)
            {
                throw new FileNotFoundException(String.Format(ErrorStrings.FailPlayAudioMissing, resource), e);
            }
            catch (InsufficientMemoryException e)
            {
                throw new InsufficientMemoryException(string.Format(ErrorStrings.FailPlayAudioMemory, resource), e);
            }
            //catch
            //{
            //    LogHelper.Log(string.Format(ErrorStrings.FailPlayAudioFile, resource), System.Diagnostics.TraceEventType.Warning, "Audio");
            //}

        }

        /// <summary>
        /// Play a resource for a set amount of loops
        /// </summary>
        /// <param name="resource">The name of the audio resource you want to play</param>
        /// <param name="loops">The number of times it should loop</param>
        public static void PlayResource(string resource, int loops)
        {
            if (String.IsNullOrEmpty(resource))
                throw new ArgumentException(ErrorStrings.ExMsgResourceNullOrEmpty, Utilities.GetVariableName(resource));

            try
            {
                System.Reflection.Assembly a = System.Reflection.Assembly.GetExecutingAssembly();
                Stream s = a.GetManifestResourceStream(resource);
                SoundPlayer player = new SoundPlayer(s);
                if (loops == 0)
                {
                    player.Play();
                }
                else
                {
                    for (int i = 0; i < loops; i++)
                    {
                        player.Play();
                    }
                }
            }
            catch (FileNotFoundException e)
            {
                throw new FileNotFoundException(String.Format(ErrorStrings.FailPlayAudioMissing, resource), e);
            }
            catch (InsufficientMemoryException e)
            {
                throw new InsufficientMemoryException(string.Format(ErrorStrings.FailPlayAudioMemory, resource), e);
            }
        }
    }
}
